package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.result.Result;
import com.sky.service.IOrderDetailService;
import com.sky.service.IOrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/order")
public class OrdersController {
    @Autowired
    private IOrdersService iOrdersService;
    @Autowired
    private IOrderDetailService iOrderDetailService;

    @GetMapping("/conditionSearch")
    public Result conditionSearch(OrdersPageQueryDTO dto){
        IPage<Orders> iPage =new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<Orders> lqw =new LambdaQueryWrapper<>();
        lqw.like(dto.getNumber()!=null,Orders::getNumber,dto.getNumber());
        lqw.like(dto.getPhone()!=null,Orders::getPhone,dto.getPhone());
        lqw.lt(dto.getEndTime()!=null,Orders::getOrderTime,dto.getEndTime());
        lqw.gt(dto.getBeginTime()!=null,Orders::getOrderTime,dto.getBeginTime());
        lqw.eq(dto.getStatus()!=null,Orders::getStatus,dto.getStatus());
        lqw.orderByDesc(Orders::getOrderTime);
        iOrdersService.page(iPage,lqw);
        List<Orders> records = iPage.getRecords();
        List<OrderVO> voList =new ArrayList<>();
        for (Orders order : records) {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(order,orderVO);
            //在获取一下 orderDishes 格式->  宫保鸡丁*3; 吃的*3;
            LambdaQueryWrapper<OrderDetail> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> list = iOrderDetailService.list(lqw1);
            List<String> dishList = list.stream().map(s -> s.getName() + "*" + s.getNumber() + ";").collect(Collectors.toList());
            orderVO.setOrderDishes(StringUtils.join(dishList," "));
            //在这一层复用的
            OrderVO detail = this.detail(order.getId()).getData();
            orderVO.setOrderDetailList(detail.getOrderDetailList());
            voList.add(orderVO);
        }

        IPage<OrderVO> voPage =new Page<>(dto.getPage(),dto.getPageSize());
        voPage.setRecords(voList);
        voPage.setTotal(iPage.getTotal());
        //返回一个iPage<OrderVo>
        return Result.success(voPage);
    }

    @GetMapping("/statistics")
    public Result statisticeVO(){
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        //待接单数量
        long toBeConfirmed = iOrdersService.count
                (new LambdaQueryWrapper<Orders>().eq(Orders::getStatus, Orders.TO_BE_CONFIRMED));
        orderStatisticsVO.setToBeConfirmed((int) toBeConfirmed);
        //待派送
        long confirmed = iOrdersService.count
                (new LambdaQueryWrapper<Orders>().eq(Orders::getStatus, Orders.CONFIRMED));
        orderStatisticsVO.setConfirmed((int)confirmed);
        //派送中
        long deliveryInProgress = iOrdersService.count
                (new LambdaQueryWrapper<Orders>().eq(Orders::getStatus, Orders.DELIVERY_IN_PROGRESS));
        orderStatisticsVO.setDeliveryInProgress((int)deliveryInProgress);
        return Result.success(orderStatisticsVO);
    }

    @GetMapping("/details/{id}")
    public Result<OrderVO> detail (@PathVariable Long id) {
        Orders order = iOrdersService.getById(id);
        LambdaQueryWrapper<OrderDetail> lqw =new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> list = iOrderDetailService.list(lqw);
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order,orderVO);
        orderVO.setOrderDetailList(list);
        return Result.success(orderVO);
    }

    @PutMapping("/delivery/{id}")
    public Result delivery (@PathVariable Long id){
        this.updateStatus(Orders.DELIVERY_IN_PROGRESS,id);
        return Result.success();
    }

    @PutMapping("/confirm")
    public Result confirm (@RequestBody Orders orders ){
        this.updateStatus(Orders.CONFIRMED,orders.getId());
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id){
        this.updateStatus(Orders.COMPLETED,id);
        return Result.success();
    }

    @PutMapping("/cancel")
    public Result cancel (@RequestBody Orders order){
        Orders orders = new Orders();
        orders.setId(order.getId());
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelReason(order.getCancelReason());
        iOrdersService.updateById(orders);
        return Result.success();
    }

    @PutMapping("/rejection")
    public Result rejection (@RequestBody Orders order){
        Orders orders = new Orders();
        orders.setId(order.getId());
        orders.setStatus(Orders.CANCELLED);
        orders.setRejectionReason(order.getRejectionReason());
        iOrdersService.updateById(orders);
        return Result.success();
    }



    private void updateStatus(Integer status, Long id){
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(status);
        iOrdersService.updateById(orders);
    }
}
